package com.example.demo.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.define.UserDefine;
import com.example.demo.impl.DahuaVideoService;
import com.example.demo.utils.SignVideoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;

/**
 * @author xumf
 * @date 2019/12/13 14:36
 */
@Service
public class VideoService {

    @Autowired
    private DahuaVideoService dahuaVideoService;

    public String getToken() throws NoSuchAlgorithmException {
        String result=dahuaVideoService.sendFirstAuthorize();
        if(result!=null){
            JSONObject object=JSON.parseObject(result);
            String randomKey= object.getString("randomKey");
            String sign= SignVideoUtils.videoSign(UserDefine.REALM_VALUE,randomKey,UserDefine.USER_VALUE,UserDefine.PASS_VALUE);
            String result2=dahuaVideoService.sendSecondAuthorize(sign,randomKey);
            if(result2!=null){
                JSONObject objectToken=JSON.parseObject(result2);
                String token= objectToken.getString("token");
                Integer duration=objectToken.getInteger("duration");
                return token;
            }
        }
        return null;
    }
}
